﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Framework_Tool
{
    /// <summary>
    /// Interaction logic for AssessmentAdd.xaml
    /// </summary>
    public partial class AssessmentAdd : Page
    {
        MainWindow main;

        public AssessmentAdd()
        {
            InitializeComponent();
            asmntIDTxt.Text = Database.NextAsmntID();
            Database.FindUnitTitle_AsmntAdd(this);

            asmntTypeCmbx_Add.SelectedItem = null;

            viewAssessmentBtn.FontWeight = FontWeights.Normal;
            addAssessmentBtn.FontWeight = FontWeights.ExtraBold;
            updateAssessmentBtn.FontWeight = FontWeights.Normal;
            deleteAssessmentBtn.FontWeight = FontWeights.Normal;
            searchAssessmentBtn.FontWeight = FontWeights.Normal;
        }

        private void viewAssessmentBtn_Click(object sender, RoutedEventArgs e)
        {
            AssessmentView asmntViewWindow = new AssessmentView();
            asmntViewWindow.Content = null;
            mainContent.Children.Add(asmntViewWindow.mainContent);
        }

        private void updateAssessmentBtn_Click(object sender, RoutedEventArgs e)
        {
            AssessmentUpdate asmntUpdateWindow = new AssessmentUpdate();
            asmntUpdateWindow.Content = null;
            mainContent.Children.Add(asmntUpdateWindow.mainContent);
        }

        private void deleteAssessmentBtn_Click(object sender, RoutedEventArgs e)
        {
            AssessmentDelete asmntDeleteWindow = new AssessmentDelete();
            asmntDeleteWindow.Content = null;
            mainContent.Children.Add(asmntDeleteWindow.mainContent);
        }

        private void searchAssessmentBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void asmntAdd_Click_1(object sender, RoutedEventArgs e)
        {
            if (asmntTypeCmbx_Add.SelectedIndex == 0 || asmntTypeCmbx_Add.SelectedIndex == 1 || asmntTypeCmbx_Add.SelectedIndex == 3)
            {
                unitNameCmbx_AsmntAdd.SelectionChanged -= unitName_AsmntAdd_SelectionChanged_1;
                Database.CreateAssessment(this);
                unitNameCmbx_AsmntAdd.SelectionChanged += unitName_AsmntAdd_SelectionChanged_1;
            }

            else if (asmntTypeCmbx_Add.SelectedIndex == 2)
            {
                MessageBoxResult mBoxResult = MessageBox.Show("Do you want to add another question?", "Multiple choice question",
                    MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                if (mBoxResult == MessageBoxResult.Yes)
                {
                    unitNameCmbx_AsmntAdd.SelectionChanged -= unitName_AsmntAdd_SelectionChanged_1;
                    Database.CreateAssessment(this);
                    unitNameCmbx_AsmntAdd.SelectionChanged += unitName_AsmntAdd_SelectionChanged_1;
                }

                else if (mBoxResult == MessageBoxResult.No)
                {
                    unitNameCmbx_AsmntAdd.SelectionChanged -= unitName_AsmntAdd_SelectionChanged_1;
                    Database.CreateAssessment(this);
                    unitNameCmbx_AsmntAdd.SelectionChanged += unitName_AsmntAdd_SelectionChanged_1;
                }

                else if (mBoxResult == MessageBoxResult.Cancel)
                {
                }
            }
        }

        private void mainContent_Loaded_1(object sender, RoutedEventArgs e)
        {
            viewAssessmentBtn.FontWeight = FontWeights.Normal;
            addAssessmentBtn.FontWeight = FontWeights.ExtraBold;
            updateAssessmentBtn.FontWeight = FontWeights.Normal;
            deleteAssessmentBtn.FontWeight = FontWeights.Normal;
            searchAssessmentBtn.FontWeight = FontWeights.Normal;
        }

        public void clearAsmntGrid()
        {
            eDetailsGrid_addAsmnt.Visibility = Visibility.Hidden;
            fitbDetailsGrid_addAsmnt.Visibility = Visibility.Hidden;
            mcDetailsGrid_addAsmnt.Visibility = Visibility.Hidden;
            wsDetailGrid_addAsmnt.Visibility = Visibility.Hidden;
        }

        private void asmntTypeCmbx_Add_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (asmntTypeCmbx_Add.SelectedIndex == 0)
            {
                clearAsmntGrid();
                essayQuTxt.Text = null;
                eKeywordTxt_addAsmnt.Text = null;
                eKeywordListBox_addAsmnt.Items.Clear();
                eDetailsGrid_addAsmnt.Visibility = Visibility.Visible;
            }

            if (asmntTypeCmbx_Add.SelectedIndex == 1)
            {
                clearAsmntGrid();
                fitbQuTxt.Text = null;
                fitbKeywordTxt.Text = null;
                fitbDetailsGrid_addAsmnt.Visibility = Visibility.Visible;
            }

            if (asmntTypeCmbx_Add.SelectedIndex == 2)
            {
                clearAsmntGrid();
                mcQuIDLbl_addAsmnt.Content = Database.NextMcQuID();
                mcQuTxt_addAsmnt.Text = null;
                mcCorrectLb.Items.Clear();
                mcAltAnsListBox.Items.Clear();
                mcDetailsGrid_addAsmnt.Visibility = Visibility.Visible;
            }

            if (asmntTypeCmbx_Add.SelectedIndex == 3)
            {
                clearAsmntGrid();
                wsIDLbl_addAsmnt.Content = Database.NextWsID();
                wsWordsListBox_addAsmnt.Items.Clear();
                wsDetailGrid_addAsmnt.Visibility = Visibility.Visible;
            }

            e.Handled = true;
        }

        private void unitName_AsmntAdd_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Database.populateUnitID_AsmntAdd(this);
        }

        private void resetAsmntAdd_Click_1(object sender, RoutedEventArgs e)
        {
            asmntTitleTxt.Text = null;
            unitNameCmbx_AsmntAdd.SelectedValue = null;
            unitIDLbl_AsmntAdd.Content = null;
            asmntTypeCmbx_Add.SelectedValue = null;
            asmntDescTxt.Text = null;
            clearAsmntGrid();
            asmntAddedLblGrid.Visibility = Visibility.Hidden;
        }

        // *********************************************** ESSAY ***********************************************

        // ******************************************* FILL IN THE BLANKS *******************************************

        // ********************************************* MULTIPLE CHOICE *********************************************

        private void mcCorrectAdd_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (String li in mcCorrectLb.Items)
            {
                if (li.Equals(mcCorrectAnsTxt_addAsmnt.Text))
                {
                    return;
                }
            }
            mcCorrectLb.Items.Add(mcCorrectAnsTxt_addAsmnt.Text);
            mcCorrectAnsTxt_addAsmnt.Text = null;
        }

        private void mcCorrectDelete_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            mcCorrectLb.Items.Remove(mcCorrectLb.SelectedItem);
        }

        private void mcAltAnsAdd_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (String li in mcAltAnsListBox.Items)
            {
                if (li.Equals(mcAltAnsTxt_addAsmnt.Text.ToString()))
                {
                    return;
                }
            }
            mcAltAnsListBox.Items.Add(mcAltAnsTxt_addAsmnt.Text.ToString());
            mcAltAnsTxt_addAsmnt.Text = null;
        }

        private void mcAltAnsDelete_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            mcAltAnsListBox.Items.Remove(mcAltAnsListBox.SelectedItem);
        }

        private void mcAsmnt_Loaded_1(object sender, RoutedEventArgs e)
        {
            Database.NextMcQuID();
        }

        // *********************************************** WORDSEARCH ***********************************************

        private void wsWordAdd_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (String li in wsWordsListBox_addAsmnt.Items)
            {
                if (li.Equals(wsAddWordTxt_addAsmnt.Text))
                {
                    return;
                }
            }
            wsWordsListBox_addAsmnt.Items.Add(wsAddWordTxt_addAsmnt.Text);
            wsAddWordTxt_addAsmnt.Text = null;
        }

        private void wsWordDelete_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
            wsWordsListBox_addAsmnt.Items.Remove(wsWordsListBox_addAsmnt.SelectedItem);
        }

        private void mcCorrectLb_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            e.Handled = true;
        }

        private void addMCQu_addAsmnt_Click_1(object sender, RoutedEventArgs e)
        {
           MessageBoxResult mBoxResult = MessageBox.Show("Do you want to add another question?", "Multiple choice question", 
               MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
            if (mBoxResult == MessageBoxResult.Yes)
            {
                //code for Yes
            }
            else if (mBoxResult == MessageBoxResult.No)
            {
                //code for No
            }
            else if (mBoxResult == MessageBoxResult.Cancel)
            {
            }
        }

    }
}
